/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Fregister.java
 *
 * Created on 2 mai 2011, 21:06:27
 */
package frame;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;
import server._ZoneExchange;
import user.User;

/**
 *
 * @author sz
 */
public class Fregister extends javax.swing.JFrame {

    /** Creates new form Fregister */
     public Fregister() {
        initComponents();
        this.setCenter();
        
    }
     
    public Fregister(_ZoneExchange zEx) {
        initComponents();
        this.setCenter();
        this.zEx=zEx;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pUserInfo = new javax.swing.JPanel();
        labelUser = new javax.swing.JLabel();
        labelSex = new javax.swing.JLabel();
        labelBirthday = new javax.swing.JLabel();
        labelEmail = new javax.swing.JLabel();
        labelTel = new javax.swing.JLabel();
        labelPwd = new javax.swing.JLabel();
        labelConfiPwd = new javax.swing.JLabel();
        textUser = new javax.swing.JTextField();
        textEmail = new javax.swing.JTextField();
        textTel = new javax.swing.JTextField();
        comboxSex = new javax.swing.JComboBox();
        textBirthday = new javax.swing.JFormattedTextField();
        textPwd = new javax.swing.JPasswordField();
        textConfirmPwd = new javax.swing.JPasswordField();
        bRegister = new javax.swing.JButton();
        bClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(126, 194, 213));
        setBounds(new java.awt.Rectangle(0, 0, 420, 350));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(255, 255, 255));
        setName("Register"); // NOI18N
        setResizable(false);

        pUserInfo.setBackground(new java.awt.Color(241, 250, 255));
        pUserInfo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(125, 220, 245)));
        pUserInfo.setLayout(null);

        labelUser.setForeground(new java.awt.Color(13, 55, 85));
        labelUser.setText("Username:");
        labelUser.setAlignmentX(20.0F);
        labelUser.setAlignmentY(40.0F);
        pUserInfo.add(labelUser);
        labelUser.setBounds(20, 40, 60, 25);

        labelSex.setForeground(new java.awt.Color(13, 55, 85));
        labelSex.setText("Sex :");
        pUserInfo.add(labelSex);
        labelSex.setBounds(20, 80, 60, 25);

        labelBirthday.setText("Birthday:");
        pUserInfo.add(labelBirthday);
        labelBirthday.setBounds(190, 120, 80, 25);

        labelEmail.setText("Email:");
        pUserInfo.add(labelEmail);
        labelEmail.setBounds(20, 120, 60, 25);

        labelTel.setText("Tel:");
        pUserInfo.add(labelTel);
        labelTel.setBounds(190, 80, 80, 25);

        labelPwd.setText("Pwd:");
        pUserInfo.add(labelPwd);
        labelPwd.setBounds(20, 160, 60, 25);

        labelConfiPwd.setText("Cofirm Pwd:");
        pUserInfo.add(labelConfiPwd);
        labelConfiPwd.setBounds(190, 160, 60, 25);
        pUserInfo.add(textUser);
        textUser.setBounds(75, 40, 100, 25);
        pUserInfo.add(textEmail);
        textEmail.setBounds(60, 120, 100, 25);
        pUserInfo.add(textTel);
        textTel.setBounds(250, 80, 100, 25);

        comboxSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "male", "female" }));
        pUserInfo.add(comboxSex);
        comboxSex.setBounds(60, 80, 100, 25);

        textBirthday.setColumns(10);
        textBirthday.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/mm/yyyy"))));
        pUserInfo.add(textBirthday);
        textBirthday.setBounds(250, 120, 100, 25);
        pUserInfo.add(textPwd);
        textPwd.setBounds(60, 160, 100, 25);
        pUserInfo.add(textConfirmPwd);
        textConfirmPwd.setBounds(250, 160, 100, 25);

        bRegister.setText("Register");
        bRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRegisterActionPerformed(evt);
            }
        });

        bClose.setText("Close");
        bClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(pUserInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(100, 100, 100)
                .addComponent(bRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(bClose, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(pUserInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bClose, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_bCloseActionPerformed

    private void bRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRegisterActionPerformed
        // TODO add your handling code here:
        String userName=textUser.getText();
        String sex=(String)comboxSex.getSelectedItem();
        String email=textEmail.getText();
        String birthDay=textBirthday.getText();
        String tel=textTel.getText();
        String pwd=new String(textPwd.getPassword());
        String pwdConfirm= new String(textConfirmPwd.getPassword());
        
        if( userName.trim().equals("") || email.trim().equals("") || birthDay.trim().equals("")
                || birthDay.trim().equals("") || pwd.trim().equals("")|| pwdConfirm.trim().equals("")
                   ){
				JOptionPane.showMessageDialog(this, "the info is not completed");
				return;
		}
	if(pwd.length()<6){
				JOptionPane.showMessageDialog(this, "password at least 6 chiffres");
				return;
	}
			if(!pwd.equals(pwdConfirm)){
				JOptionPane.showMessageDialog(this, "password 2 times is not the same");
				return;
			}
			User tmp=new User();
                        tmp.setUserName(userName);
                        tmp.setSex(sex);
                        tmp.setEmail(email);
                        tmp.setBirthDay(birthDay);
                        tmp.setTelNumber(tel);
			try {   
                                String id = zEx.registerUser(tmp, pwd);
				if(id!=null)
                                {
                                JOptionPane.showMessageDialog(this,"Congratulations，your id is："+ id +",password is："+
						pwd +"Please keep it carefully ","Succesful",JOptionPane.INFORMATION_MESSAGE);
				this.dispose();
                                }
				else
                                {
                                JOptionPane.showMessageDialog(this, "Try !!");
                                }    
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				JOptionPane.showMessageDialog(this, "Register fail try later");
				this.dispose();
				return;
			}
        
        
    }//GEN-LAST:event_bRegisterActionPerformed
   
     private void setCenter() {
		Dimension sizeScreen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension sizeAppli = getSize();
		setLocation((sizeScreen.width - sizeAppli.width) / 2,
				(sizeScreen.height - sizeAppli.height) / 2);
	}
     
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Fregister().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bClose;
    private javax.swing.JButton bRegister;
    private javax.swing.JComboBox comboxSex;
    private javax.swing.JLabel labelBirthday;
    private javax.swing.JLabel labelConfiPwd;
    private javax.swing.JLabel labelEmail;
    private javax.swing.JLabel labelPwd;
    private javax.swing.JLabel labelSex;
    private javax.swing.JLabel labelTel;
    private javax.swing.JLabel labelUser;
    private javax.swing.JPanel pUserInfo;
    private javax.swing.JFormattedTextField textBirthday;
    private javax.swing.JPasswordField textConfirmPwd;
    private javax.swing.JTextField textEmail;
    private javax.swing.JPasswordField textPwd;
    private javax.swing.JTextField textTel;
    private javax.swing.JTextField textUser;
    // End of variables declaration//GEN-END:variables

    private _ZoneExchange zEx=null;
}
